<?php

/**
 * --------------------------------
 * Default TeacherClassesController
 *
 * 教师班级管理的统一类  TeacherClassesController
 * 
 * --------------------------------
 */

class TeacherClassesController extends BaseController{

    /**
  	 * 教师所教班级搜索接口 
  	 * @param  $id
  	 * @return Response
  	 */
    public function index($teacher_id){

    	$classes_result = DB::table('teacher_classes')
    						->join('classes','classes.id','=','teacher_classes.classes_id')
    						->join('teacher','teacher_classes.teacher_id','=','teacher.id')
    						->select('teacher_classes.*','teacher.name as teacher_name',
    								 'classes.grade as classes_grade','classes.class as classes_class')
    						->where('teacher_classes.teacher_id',$teacher_id)
    						->get();
    						
    	if(!$classes_result){
    		return Response::json(['error'=>true,'message'=>'记录不存在'],201);
    	}else{
    		return Response::json(['error'=>false,'result'=>$classes_result],200);
    	}                    
    }

    /**
     * 给教师添加班级
     * @param  $id
     * @return Response
     */
    public function store($teacher_id){

        $classes_id = Input::get('classes_id');
        if(!$classes_id){
            return Response::json(['error'=>true,'message'=>'缺少重要参数'],201);
        }
        $id = DB::table('teacher_classes')->insertGetId(array('teacher_id'=>$teacher_id,
                                                              'classes_id'=>$classes_id));
        if(!$id){
            return Response::json(['error'=>true,'message'=>'添加失败'],201);
        }else{
            return Response::json(['fasle'=>true,'message'=>'添加成功','result'=>$id],200);
        }
    }
    
    /**
     * 删除教师与班级的关联
     * @param  $id
     * @return Response
     */
    public function destroy($teacher_id,$classes_id){
        $result = DB::table('teacher_classes')->where('teacher_id',$teacher_id)
                    ->where('classes_id',$classes_id)->delete();
        if(!$result){
            return Response::json(['error'=>true,'message'=>'删除失败'],201);
        }else{
             return Response::json(['fasle'=>false,'message'=>'删除成功'],200);
        }
    }
} 